﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace spktNhom4DoAnCuoiKy.ManagerModules
{
    public partial class ManageAuctionSession : System.Web.UI.Page
    {
        AuctionOnlineDataContext db = new AuctionOnlineDataContext();
        void FillGirdView()
        {
           
            
            if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
            {

                 var data = db.PhienDauGias.Select(x => new { x.MaPhien,NhomPhien = Session.getResourceManager().GetString(x.NhomLoaiPhien.NhomPhien.TenNhomPhien),LoaiPhien=Session.getResourceManager().GetString( x.NhomLoaiPhien.LoaiPhien.TenLoaiPhien),TinhTrang =  x.TinhTrangPhien.MoTa,NguoiDangTin = x.TenNguoiDangTin,ThoiGianKetThuc= x.ThoiGianKetThucBan }).OrderBy(ViewState["SortExpression"].ToString(), ViewState["SortDirection"].ToString() == "ASC" ? SortDirection.Ascending : SortDirection.Descending).ToList();
                 gvPhien.DataSource = data;
                
            }
            else
            {
                var data = db.PhienDauGias.Select(x => new { x.MaPhien, NhomPhien = Session.getResourceManager().GetString(x.NhomLoaiPhien.NhomPhien.TenNhomPhien), LoaiPhien = Session.getResourceManager().GetString(x.NhomLoaiPhien.LoaiPhien.TenLoaiPhien), TinhTrang = x.TinhTrangPhien.MoTa, NguoiDangTin = x.TenNguoiDangTin, ThoiGianKetThuc = x.ThoiGianKetThucBan }).ToList();
                gvPhien.DataSource = data;
            }

            gvPhien.DataBind();
            ControlGoToPage.PageCount = gvPhien.PageCount;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            int rowPerPage = int.Parse(ddlSoPhienTrenTrang.SelectedValue);
            if (IsPostBack) Session["RowPerPage"] = rowPerPage.ToString();

            if (Session["RowPerPage"] != null)
                rowPerPage = int.Parse(Session["RowPerPage"].ToString());
            else Session["RowPerPage"] = rowPerPage;
            ddlSoPhienTrenTrang.SelectedValue = rowPerPage.ToString();
            gvPhien.PageSize = rowPerPage;
            FillGirdView();
        }

        protected void gvPhien_RowCreated(object sender, GridViewRowEventArgs e)
        {
            Label lbl = (Label)e.Row.FindControl("STT");
            if (lbl != null)
            {
                lbl.Text = (gvPhien.PageIndex * gvPhien.PageSize + e.Row.RowIndex + 1).ToString();
               
            }
        }
        private SortDirection GetSortDirection(string column)
        {

            // By default, set the sort direction to ascending.
            string sortDirection = "ASC";

            // Retrieve the last column that was sorted.
            string sortExpression = ViewState["SortExpression"] as string;

            if (sortExpression != null)
            {
                // Check if the same column is being sorted.
                // Otherwise, the default value can be returned.
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }

            // Save new values in ViewState.
            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;
            if (sortDirection == "ASC") return SortDirection.Ascending;
            return SortDirection.Descending;
        }
        private int getColumnIndexByName(GridView gridview, string name)
        {
            for (int i = 0; i < gridview.Columns.Count; i++)
            {
                if (gridview.Columns[i].SortExpression.ToLower() == name.ToLower().Trim())
                {
                    return i;
                }
                gridview.Columns[i].HeaderText = removeUpDownArrow(gridview.Columns[i].HeaderText);
            }
            return -1;
        }
        private string removeUpDownArrow(string s)
        {
            return s.Replace("↑", "").Replace("↓", "");
        }
        protected void gvPhien_Sorting(object sender, GridViewSortEventArgs e)
        {
            AuctionOnlineDataContext db = new AuctionOnlineDataContext();
            SortDirection sortd;
            var data = db.PhienDauGias.Select(x => new { x.MaPhien, NhomPhien = Session.getResourceManager().GetString(x.NhomLoaiPhien.NhomPhien.TenNhomPhien), LoaiPhien = Session.getResourceManager().GetString(x.NhomLoaiPhien.LoaiPhien.TenLoaiPhien), TinhTrang = x.TinhTrangPhien.MoTa, NguoiDangTin = x.TenNguoiDangTin, ThoiGianKetThuc = x.ThoiGianKetThucBan }).OrderBy(e.SortExpression, sortd = GetSortDirection(e.SortExpression)).ToList();

            int indexColumnSort = getColumnIndexByName(gvPhien, e.SortExpression);
            if (sortd == SortDirection.Ascending)
            {
                gvPhien.Columns[indexColumnSort].HeaderText = removeUpDownArrow(gvPhien.Columns[indexColumnSort].HeaderText) + "↑";
            }
            else
            {
                gvPhien.Columns[indexColumnSort].HeaderText = removeUpDownArrow(gvPhien.Columns[indexColumnSort].HeaderText) + "↓";
            }

            gvPhien.DataSource = data;
            gvPhien.DataBind();
        }
        protected void GoToPage_Clicked(object sender, GoClickedEventArgs e)
        {
            //if (e.NextPage - 1 > gvQuanLyNguoiDung.PageCount)
            //{
            //    Response.Write("<script>alert('số bạn nhập vượt quá số trang.');</script>");
            //    return;
            //}

            gvPhien.PageIndex = e.NextPage - 1;
            gvPhien.DataBind();
        }

        protected void ddlSoPhienTrenTrang_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvPhien.PageSize = int.Parse(ddlSoPhienTrenTrang.SelectedValue);
            FillGirdView();
        }

     

        

        protected void gvPhien_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPhien.PageIndex = e.NewPageIndex;
            gvPhien.DataBind();
        }
    }
}